<!-- 预约总览组件 -->
<template>
  <div class="calendar-container">
    <!-- 第一行：日期显示和回到今日按钮 -->
    <div class="calendar-header">
      <div class="current-date">
        {{ formatDate(selectedDate) }}
      </div>
      <div class="back-to-today" @click="backToToday">回到今日</div>
    </div>

    <!-- 第二行：日历 -->
    <div class="calendar-main">
      <div class="calendar-wrapper">
        <!-- 左侧导航按钮 -->
        <div class="nav-button left" @click="previousDay">
          <el-icon><ArrowLeft /></el-icon>
        </div>

        <!-- 日历内容区域 -->
        <div class="calendar-content">
          <!-- 星期表头 -->
          <div class="week-header">
            <div class="week-day" v-for="day in weekDays" :key="day">
              {{ day }}
            </div>
          </div>

          <!-- 日期网格 -->
          <div class="date-grid">
            <div
              v-for="date in calendarDates"
              :key="date.dateString"
              :class="[
                'date-cell',
                {
                  selected: date.dateString === selectedDateString,
                  today: date.dateString === todayString,
                  'other-month': !date.isCurrentMonth,
                },
              ]"
              @click="selectDate(date.dateString)">
              {{ date.day }}
            </div>
          </div>
        </div>

        <!-- 右侧导航按钮 -->
        <div class="nav-button right" @click="nextDay">
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div>
    </div>

    <!-- 第三行：预约信息展示区 -->
    <div class="appointment-section">
      <div class="appointment-title">当日预约</div>
      <div class="appointment-list">
        <div
          v-for="appointment in currentAppointments"
          :key="appointment.id"
          class="appointment-item">
          <div class="appointment-time">{{ appointment.time }}</div>
          <div class="appointment-info">
            {{ appointment.type }} 预约人姓名：{{
              appointment.elderName
            }}
            预约人手机号：{{ appointment.phone }}
          </div>
        </div>
        <div v-if="currentAppointments.length === 0" class="no-appointment">
          暂无预约信息
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
import { ArrowLeft, ArrowRight } from "@element-plus/icons-vue";

// 接收父组件传递的预约数据
const props = defineProps({
  appointmentData: {
    type: Object,
    default: () => ({}),
  },
});

// 星期数组
const weekDays = ["日", "一", "二", "三", "四", "五", "六"];

// 当前选中的日期
const selectedDate = ref(new Date());
const today = ref(new Date());

// 格式化日期显示
const formatDate = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const weekDay = weekDays[date.getDay()];
  return `${year}年${month}月${day}日 星期${weekDay}`;
};

// 获取日期字符串 (YYYY-MM-DD格式)
const getDateString = (date) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
};

// 计算属性
const selectedDateString = computed(() => getDateString(selectedDate.value));
const todayString = computed(() => getDateString(today.value));

// 生成日历日期数组（一周7天，以选中日期为中心）
const calendarDates = computed(() => {
  const dates = [];
  const currentDate = new Date(selectedDate.value);
  const currentMonth = currentDate.getMonth();

  // 获取选中日期是星期几
  const selectedDayOfWeek = currentDate.getDay();

  // 计算这一周的开始日期（周日为0）
  const weekStart = new Date(currentDate);
  weekStart.setDate(currentDate.getDate() - selectedDayOfWeek);

  // 生成一周的7天
  for (let i = 0; i < 7; i++) {
    const date = new Date(weekStart);
    date.setDate(weekStart.getDate() + i);

    dates.push({
      day: date.getDate(),
      dateString: getDateString(date),
      isCurrentMonth: date.getMonth() === currentMonth,
    });
  }

  return dates;
});

// 当前选中日期的预约信息
const currentAppointments = computed(() => {
  const appointments = props.appointmentData[selectedDateString.value] || [];
  // 按时间排序
  return appointments.sort((a, b) => a.time.localeCompare(b.time));
});

// 方法
const selectDate = (dateString) => {
  selectedDate.value = new Date(dateString);
};

const backToToday = () => {
  selectedDate.value = new Date();
};

const previousDay = () => {
  const newDate = new Date(selectedDate.value);
  newDate.setDate(newDate.getDate() - 1);
  selectedDate.value = newDate;
};

const nextDay = () => {
  const newDate = new Date(selectedDate.value);
  newDate.setDate(newDate.getDate() + 1);
  selectedDate.value = newDate;
};

onMounted(() => {
  // 组件挂载时设置为今天
  selectedDate.value = new Date();
});
</script>

<style scoped lang="scss">
.calendar-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-height: 260px;
  gap: 8px;
  margin-top: 20px;
}

.calendar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .current-date {
    font-size: 16px;
    font-weight: bold;
    color: #333;
  }

  .back-to-today {
    color: #409eff;
    cursor: pointer;
    font-size: 14px;

    &:hover {
      text-decoration: underline;
    }
  }
}

.calendar-main {
  .calendar-wrapper {
    display: flex;
    align-items: center;
    gap: 12px;

    .nav-button {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 28px;
      height: 28px;
      border-radius: 50%;
      background-color: #f0f0f0;
      cursor: pointer;
      transition: background-color 0.3s;
      flex-shrink: 0;

      &:hover {
        background-color: #e0e0e0;
      }

      .el-icon {
        font-size: 16px;
        color: #666;
      }
    }

    .calendar-content {
      flex: 1;
      display: flex;
      flex-direction: column;
      gap: 4px;

      .week-header {
        display: grid;
        grid-template-columns: repeat(7, 1fr);
        gap: 1px;

        .week-day {
          text-align: center;
          padding: 4px 2px;
          font-size: 12px;
          color: #666;
          background-color: #f5f5f5;
        }
      }

      .date-grid {
        display: grid;
        grid-template-columns: repeat(7, 1fr);
        gap: 2px;

        .date-cell {
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
          font-size: 14px;
          border-radius: 4px;
          transition: all 0.3s;
          height: 32px;
          min-width: 32px;

          &:hover {
            background-color: #f0f8ff;
          }

          &.selected {
            background-color: #409eff;
            color: white;
          }

          &.today {
            background-color: #e6f7ff;
            color: #1890ff;
            font-weight: bold;
          }

          &.other-month {
            color: #ccc;
          }
        }
      }
    }
  }
}

.appointment-section {
  flex: 1;
  display: flex;
  flex-direction: column;

  .appointment-title {
    font-size: 14px;
    font-weight: bold;
    color: #333;
    margin-bottom: 8px;
  }

  .appointment-list {
    flex: 1;
    max-height: 80px;
    overflow-y: auto;

    .appointment-item {
      padding: 8px 0;
      border-bottom: 1px solid #f0f0f0;

      &:last-child {
        border-bottom: none;
      }

      .appointment-time {
        font-size: 16px;
        font-weight: bold;
        color: #333;
        margin-bottom: 4px;
      }

      .appointment-info {
        font-size: 12px;
        color: #666;
        line-height: 1.4;
      }
    }

    .no-appointment {
      text-align: center;
      color: #999;
      font-size: 14px;
      padding: 20px 0;
    }
  }
}

// 滚动条样式
.appointment-list::-webkit-scrollbar {
  width: 4px;
}

.appointment-list::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 2px;
}

.appointment-list::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 2px;
}

.appointment-list::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
</style>
